public class MemoryUsage {
    public static void main(String[] args) {
        // 获取Runtime实例
        Runtime runtime = Runtime.getRuntime();
        
        // 运行垃圾回收
        runtime.gc();
        
        // 获取内存信息
        long totalMemory = runtime.totalMemory();    // 总内存
        long freeMemory = runtime.freeMemory();      // 空闲内存
        long usedMemory = totalMemory - freeMemory;  // 已使用内存
        long maxMemory = runtime.maxMemory();        // 最大可用内存
        
        System.out.println("总内存: " + totalMemory / (1024 * 1024) + " MB");
        System.out.println("空闲内存: " + freeMemory / (1024 * 1024) + " MB");
        System.out.println("已使用内存: " + usedMemory / (1024 * 1024) + " MB");
        System.out.println("最大内存: " + maxMemory / (1024 * 1024) + " MB");
    }
}